﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using MOJsdk;
using System.Collections.ObjectModel;
using System.Threading;
using System.Windows.Threading;

namespace MOJ
{
    /// <summary>
    /// Interaction logic for Control_ImportImage.xaml
    /// </summary>
    public partial class Control_ImportImage : UserControl
    {
        private ObservableCollection<MojWebImage> _imgs;

        public Control_ImportImage()
        {
            InitializeComponent();
            INI();
        }

        private void INI()
        {
            INIElements();
            INIEvents();
        }
        private void INIElements()
        {
            _Grid.ApplyStyle("0101049");
            _btn_select.ApplyStyle("0101050");
            _listView.ApplyStyle("0101051");

            _Grid_Foreground.ApplyStyle("0101052");
            _progressBar_await.ApplyStyle("0101053");

            _btn_select.IsEnabled = false;
        }
        private void INIEvents()
        {
            OnImageSelected += delegate { };
            _btn_select.Click += BtnSelectedClick;
            _listView.SelectionChanged += ListViewSelectionXhanged;
        }

        private void BtnSelectedClick(object sender, RoutedEventArgs e)
        {
            int x = _listView.SelectedIndex;
            if (x == -1)
                return;

            OnImageSelected(_imgs[x]);
        }
        private void ListViewSelectionXhanged(object sender, SelectionChangedEventArgs e)
        {
            _btn_select.IsEnabled = _listView.SelectedIndex != -1;
        }

        private void AwaitState(bool IsWait)
        {
            if (IsWait)
            {
                _Grid_Foreground.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                _Grid_Foreground.Visibility = System.Windows.Visibility.Hidden;
            }
        }

        public void LoadImages(string MovieCode)
        {
            AwaitState(true);
            _listView.ItemsSource = null;
            ThreadStartString th = new ThreadStartString(LoadImagesAsync);
            th.BeginInvoke(MovieCode, null, null);
        }
        private void LoadImagesAsync(string MovieCode)
        {

            MojWebMovie m = new MojWebMovie(new TheMovieDBV3.TmdbMovie(MovieCode));
            _imgs = new ObservableCollection<MojWebImage>(m.Posters);
            foreach (var v in _imgs)
                v.DownloadFiles();

            Dispatcher.BeginInvoke(DispatcherPriority.Send, (ThreadStart)delegate
            {
                AwaitState(false);
                _listView.ItemsSource = _imgs;
            });
        }
        private delegate void ThreadStartString(string data);

        public delegate void ImageSelected(MojWebImage WebImage);
        public event ImageSelected OnImageSelected;
    }
}
